package cn.like.satoken.core;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.SaTokenException;
import cn.like.base.support.result.Rest;
import cn.like.common.handler.DefaultGlobalExceptionHandler;
import cn.like.util.StrPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author like 980650920@qq.com
 * @date 2021/7/10 21:28
 */
@RestControllerAdvice
@Slf4j
public class SatokenExceptionHandler extends DefaultGlobalExceptionHandler {

    @ExceptionHandler(NotLoginException.class)
    public Rest<String> bizException(NotLoginException ex, HttpServletRequest request) {
        return Rest.of(StrPool.EMPTY, Long.parseLong(ex.getType()), ex.getMessage()).path(request.getRequestURI());
    }

    @ExceptionHandler(SaTokenException.class)
    public Rest<String> bizException(SaTokenException ex, HttpServletRequest request) {
        return Rest.of(StrPool.EMPTY, -1, ex.getMessage()).path(request.getRequestURI());
    }
}
